﻿using System;
using System.Windows.Media;

namespace Red.Toolkit.Profiler.Logic
{
    public enum ValueType
    {
        Generic,        // no unit
        GenericInt,     // no unit (only integers)
        Time,           // seconds
        DataSpeed,      // bytes / second
        DataSize,       // bytes
    }

    public class Utils
    {
        static public Random RandomGen = new Random();

        static public Color RandomColor( double minSaturation, double maxSaturation, double minValue, double maxValue )
        {
            return Core.ColorHelpers.HsvToColor(
                (float)( RandomGen.NextDouble() * 360.0),
                (float)( minSaturation + ( maxSaturation - minSaturation ) * RandomGen.NextDouble() ),
                (float)( minValue + ( maxValue - minValue ) * RandomGen.NextDouble() ) );
        }

        static public string FormatValue( double value, Logic.ValueType type )
        {
            if ( type == Logic.ValueType.Generic )
            {
                if ( value > 1.0e9 )
                    return string.Format( "{0:F3}G", value / 1.0e9 );
                if ( value > 1.0e6 )
                    return string.Format( "{0:F3}M", value / 1.0e6 );
                if ( value > 1.0e3 )
                    return string.Format( "{0:F3}K", value / 1.0e3 );
                return string.Format( "{0:F3}", value );
            }
            else if ( type == Logic.ValueType.GenericInt )
            {
                if ( value > 1.0e9 )
                    return string.Format( "{0:F3}G", value / 1.0e9 );
                if ( value > 1.0e6 )
                    return string.Format( "{0:F3}M", value / 1.0e6 );
                if ( value > 1.0e3 )
                    return string.Format( "{0:F3}K", value / 1.0e3 );
                return string.Format( "{0:0}", value );
            }
            else if ( type == Logic.ValueType.DataSize )
            {
                if ( value > 1024.0 * 1024.0 * 1024.0 )
                    return string.Format( "{0:F3}GB", value / (1024.0 * 1024.0 * 1024.0) );
                if ( value > 1024.0 * 1024.0 )
                    return string.Format( "{0:F3}MB", value / (1024.0 * 1024.0) );
                if ( value > 1024.0 )
                    return string.Format( "{0:F3}KB", value / 1024.0 );
                return string.Format( "{0}B", value );
            }
            else if ( type == Logic.ValueType.DataSpeed )
            {
                if ( value > 1024.0 * 1024.0 * 1024.0 )
                    return string.Format( "{0:F3}GB/s", value / (1024.0 * 1024.0 * 1024.0) );
                if ( value > 1024.0 * 1024.0 )
                    return string.Format( "{0:F3}MB/s", value / (1024.0 * 1024.0) );
                if ( value > 1024.0 )
                    return string.Format( "{0:F3}KB/s", value / 1024.0 );
                return string.Format( "{0:F2}B/s", value );
            }
            else if ( type == Logic.ValueType.Time )
            {
                if ( value > 60.0 )
                {
                    int sec = (int)(Math.Abs( value )) % 60;
                    int min = (int)(value) / 60;
                    return string.Format( "{0:00}:{1:F2}", min, sec );
                }
                if ( value > 1.0 )
                    return string.Format( "{0:F3}s", value );
                if ( value > 1.0e-3 )
                    return string.Format( "{0:F3}ms", value * 1.0e3 );
                if ( value > 1.0e-6 )
                    return string.Format( "{0:F3}us", value * 1.0e6 );
                return string.Format( "{0:F3}ns", value * 1.0e9 );
            }

            return string.Format( "{0:0.000}", value );
        }
    }
}
